package com.atsmart.app.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.atsmart.app.BizApp;
import com.atsmart.app.R;
import com.atsmart.app.model.TaskInfo;
import com.atsmart.app.model.TaskListInfo;
import com.atsmart.app.sql.TaskInfoSqlHelper;
import com.atsmart.app.sql.TempFromTaskSql;
import com.atsmart.app.utils.StringUtil;
import com.baidu.mapapi.s;

public class TaskListAdapter extends LoadingAdapter {
	private static final int VIEWTYPE_OVERDUE = 1;
	private static final int VIEWTYPE_NORMAL = 2;
	private static final int VIEWTYPE_LOADING = 3;

	private TaskListInfo datasource;
	private LayoutInflater inflater;
	private TempFromTaskSql temsql;
	private TaskInfoSqlHelper sql;
	public TaskListAdapter(TaskListInfo datasource) {
		this.datasource = datasource;
		inflater = (LayoutInflater) BizApp.getInstance().getSystemService(
				Context.LAYOUT_INFLATER_SERVICE);
		sql = new TaskInfoSqlHelper();
		sql.open(BizApp.getInstance().getSqlHelper());
		temsql = new TempFromTaskSql();
		temsql.open(BizApp.getInstance().getSqlHelper());

	}

	public void appendDataSource(TaskListInfo appendData) {
		datasource.getTasks().addAll(appendData.getTasks());
	}

	private static class ViewHolder {
		TextView title;
		TextView desc;
		TextView num;
	}

	@Override
	public int getItemViewType(int position) {
		if (delegate != null && delegate.needLoading()
				&& position == getCount() - 1) {
			return VIEWTYPE_LOADING;
		}
		TaskInfo taskInfo = datasource.getTasks().get(position);
		String time = taskInfo.getDate();
		String qti = "";
		if (time != null && time.length() > 10) {
			qti = time.substring(11, time.length());
		}
		if ("00:00:00.0".equals(qti)) {
			time = StringUtil.getFormTaskTime(taskInfo.getDate())
					+ " 23:59:59.000";
		} else {
		}
		if (StringUtil.isOverDue(time)) {
			return VIEWTYPE_OVERDUE;
		} else {
			return VIEWTYPE_NORMAL;
		}
	}

	@Override
	public int getViewTypeCount() {
		return 4;
	}

	@Override
	public int getCount() {
		if (datasource == null || datasource.getTasks() == null
				|| datasource.getTasks().size() == 0) {
			return 0;
		}

		if (delegate != null && delegate.needLoading()) {
			return datasource.getTasks().size() + 1;
		} else {
			return datasource.getTasks().size();
		}
	}

	@Override
	public Object getItem(int position) {
		if (datasource.getTasks() == null
				|| position == datasource.getTasks().size() && delegate != null
				&& delegate.needLoading()) {
			return null;
		}
		return datasource.getTasks().get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View loadingRow = super.getView(position, convertView, parent);
		if (loadingRow != null) {
			return loadingRow;
		}
		return getView(position, convertView);
	}

	private View getView(int position, View convertView) {
		ViewHolder vh;
		if (convertView == null || convertView.getTag() == null) {
			convertView = inflater
					.inflate(R.layout.task_list_item_layout, null);
			vh = new ViewHolder();
			vh.title = (TextView) convertView.findViewById(R.id.title);
			vh.desc = (TextView) convertView.findViewById(R.id.desc);
			vh.num = (TextView) convertView.findViewById(R.id.txt_task_tmp_num);
			convertView.setTag(vh);
		} else {
			vh = (ViewHolder) convertView.getTag();
		}
		TaskInfo taskInfo = datasource.getTasks().get(position);
		String time = taskInfo.getDate();
		String qti = "";
		if (time != null && time.length() > 10) {
			qti = time.substring(11, time.length());
		}
		if ("00:00:00.0".equals(qti)) {
			time = StringUtil.getFormTaskTime(taskInfo.getDate());
		} else {
			time = StringUtil.getTaskTime(taskInfo.getDate());
		}
		vh.title.setText(time + " " + taskInfo.getName());
		vh.desc.setText(taskInfo.getContent());
		if (getItemViewType(position) == VIEWTYPE_NORMAL) {
			convertView.setBackgroundResource(0);
		} else {
			convertView.setBackgroundResource(R.color.bottom_gray);
		}
		if (taskInfo.getTemplateNum() == 0) {
			vh.num.setVisibility(View.INVISIBLE);
		} else {
			 if((BizApp.getInstance().isWiFiActive() ||BizApp.getInstance().isNetworkAvailable() ) && BizApp.getInstance().keeping){
				vh.num.setVisibility(View.VISIBLE);
				vh.num.setText(String.valueOf(taskInfo.getTemplateNum()));
			 } else {
				vh.num.setVisibility(View.VISIBLE);
//				vh.num.setText(String.valueOf(sql.getCount(taskInfo.getId())));
				if(temsql.getCount(taskInfo.getId()) != 0){
					vh.num.setText(String.valueOf(temsql.getCount(taskInfo.getId())));
				}else{
					vh.num.setText(String.valueOf(sql.getTemNumByTaskId(taskInfo.getId())));
				}
			}
		}
		return convertView;
	}

	@Override
	protected boolean isLoadingRow(int position) {
		return position >= datasource.getTasks().size();
	}

}
